<template>
  <view class="container">
    <image
      class="login-img"
      src="/static/images/ygs_dlyz.png"
      mode="widthFix"
    />
    <button
      class="btn-login btn-login-phone"
      open-type="getPhoneNumber"
      @getphonenumber="getPhoneNumber"
    >
      手机号快捷登录
    </button>
      <!--     <button class="btn-login btn-login-one" v-on:tap="loginByCode">
      手机验证码登录
    </button> -->
    <view class="privacy">
      <u-checkbox
        v-model="agreement"
        :checked="agreement"
        @change="checkboxChange"
        size="14"
        active-color="#ED174D"
      />
      <view>
        我已阅读、理解并同意
        <text class="nav" v-on:tap="goToUrl(1)">《隐私政策》</text>
        <text class="nav" v-on:tap="goToUrl(2)">《用户服务协议》</text>
      </view>
    </view>
  </view>
</template>
<script>
import { reqPost } from "../../common/http.request";
import { useShop } from "../../store/useShop";
import { useUser } from "../../store/useUser";
import {
  uGetStorageSync,
  uNavigateBack,
  uNavigateTo,
  uSetStorageSync,
  uToast,
} from "../../utils/uniUtils";
export default {
  data() {
    return {
      agreement: false,
    };
  },
  methods: {
    // 勾选版权协议
    checkboxChange(e) {
      this.agreement = e;
    },
    loginByCode() {
      uNavigateTo("/pages/login/loginByPhone");
    },
    async getPhoneNumber(e) {
      var openId = uGetStorageSync("userInfo")?.openId;
      if (!this.agreement) {
        uToast("请同意服务协议!", "error");
        return;
      }

      if (e.mp.detail.errMsg == "getPhoneNumber:ok") {
        const { cloudID, code, encryptedData, iv } = e.mp.detail;
        //获取当前帐号信息，可以返回小程序的Appid
        const accountInfo = uni.getAccountInfoSync();
        let m_data = {
          // cloudID,
          code,
          // encryptedData,
          // iv,
          openId,
          appId: accountInfo.miniProgram.appId,
          ...uGetStorageSync("m_store_info")
        };
        let dt = (await reqPost("LoginByPhoneOneKey", m_data)).Data[0];
        useUser().login(dt);
        useUser().hasLogin = true; //修改登录状态， 表示已登录
        useShop().GetShoppingCart();
        uSetStorageSync("hasLogin", true);
        uNavigateBack();
      }
    },
    goToUrl(l) {
      uNavigateTo("/pages/article/" + (l == 1 ? "privacy" : "agreement"));
    },
  },
};
</script>
<style lang="scss" scoped>
.container {
  height: 100vh;
  background-color: #fff;
  display: flex;
  flex-direction: column;
  align-items: center;
  .login-img {
    margin-top: 10px;
    width: 530rpx;
  }

  .btn-login {
    width: 80%;
    margin-top: 20px;
    text-align: center;
    font-weight: bold;
    border-radius: 20px;
    border: none;

    &-phone {
      background-color: #ff2838;
      color: #fff;
    }
    &-one {
      background-color: #f2f4f7;
      color: #141414;
    }
  }

  .privacy {
    width: 80%;
    display: flex;
    margin-top: 20px;
    color: #333333;

    .nav {
      color: #ff1f3d;
    }
  }
}
</style>
